<?php

namespace app\common\model\shop;

use app\common\model\BaseModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\facade\Db;

/**
 * 店铺商品库存信息
 * Class ShopStock
 * @package app\common\model\shop
 */
class ShopStockModel extends BaseModel
{
    //
    protected $name = 'shop_stock';

    // 设置字段信息
    protected $schema = [
        'id' => 'int',        //
        'mer_id' => 'string',        // 所属商户
        'shop_id' => 'int',        // 所属店铺
        'type' => 'int',        // 类型, 1虚码  2实码
        'product_code' => 'string',        // 商品code
        'in_num' => 'int',        // 进货量
        'out_num' => 'int',        // 销售量
        'sum_num' => 'int',        // 当前库存
        'warn_flag' => 'int',        // 是否预警  1 预警 2忽略
        'update_at' => 'datetime',        // 更新时间
    ];

    /**
     * 更新门店库存
     * @param $shop_id | 门店id
     * @param $mer_id |商户id
     * @param $product_data |需变换数量
     * @return void
     * @throws Exception
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function updateShopStock(string $mer_id, int $shop_id, array $product_data, bool $dec = true)
    {
        $db = $this->db();

        //$shop_salesman = $db->where(['mer_id' => $mer_id, 'shop_id' => $shop_id])->select();
        $map = [
            'mer_id' => $mer_id,
            'shop_id' => $shop_id,
        ];
        foreach ($product_data as $v) {
            $db->removeOption()->where($map)->where('product_code', $v['code'])->update([
                    'out_num' => Db::raw('out_num ' . ($dec ? '+' : '-') . $v['num']),
                    'sum_num' => Db::raw('sum_num ' . ($dec ? '-' : '+') . $v['num'])
                ]
            );
        }
    }


    /**
     * 门店库存验证,获取库存信息
     * @param string $mer_id | 门店id
     * @param int $shop_id |商户id
     * @param array $product_list | 扫码商品列表
     * @throws Exception
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function validShopStoke(string $mer_id, int $shop_id, array $product_list): bool
    {
        if (empty($product_list)) {
            throw  new Exception('请扫商品码后,再提交.', API_ERROR);
        }
        //门店库存限制
        $shop_stock = $this->db()->where(['mer_id' => $mer_id, 'shop_id' => $shop_id])
            ->where('product_code', 'in', array_column($product_list, 'code'))
            ->column('sum_num', 'product_code');

        if (empty($shop_stock)) {
            throw  new Exception('库存数量不足,请联系经销商.', API_ERROR);
        }

        // 判断库存是否充足
        foreach ($product_list as $row) {
            if (!isset($shop_stock[$row['code']]) || (int)$row['num'] > $shop_stock[$row['code']]) {
                throw  new Exception($row['code'] . '|' . $row['product_title'] . '库存数量不足,请联系经销商.', API_ERROR);
            }
        }
        return true;
    }


}
